<script>
import axios from "axios";

export default {
  name: "FriendMessage",
  props: ['id'],
  data() {
    return {
      list: [],
      input: "",
      loading: false
    }
  },
  methods: {
    leave() {
      if (this.input !== '') {
        console.log(this.id)
        let formData = new FormData()
        formData.append("fid", this.id)
        formData.append("content", this.input)
        axios.post("/api/message/", formData)
            .then(res => {
              if (res.data.code === 0) {
                this.input = ""
                this.$message.success(res.data.msg)
                this.getList()
              } else {
                this.$message.warning(res.data.msg)
              }
            }).catch(err => this.$message.error(err))
      } else {
        this.$message.warning("请输入内容")
      }
    },
    getList() {
      this.loading = true
      axios.get(`/api/message/${this.id}`)
          .then(res => {
            if (res.data.code === 0) {
              this.loading = false
              this.list = res.data.data
            } else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    },
    del(id) {
      axios.get(`/api/message/del/${id}`)
          .then(res => {
            if (res.data.code === 0) {
              this.$message.success(res.data.msg)
              this.getList()
            } else {
              this.$message.warning(res.data.msg)
            }
          }).catch(err => this.$message.error(err))
    }
  },
  watch: {
    id: {
      immediate: true,
      handler(value) {
        if (value !== '')
          this.getList()
      }
    }
  }
}
</script>

<template>
  <div v-loading="loading">

    <div>
      <div class="message_box">
        <h1 v-if="list.length === 0" style="margin-left: 20px;color: #cdcdcd">共收到0条留言</h1>
        <div class="message_item" v-for="(item,index) in list" :key="index">
          <div style="position: relative">
            <img class="one" :src="require('@/assets/img/第一.png')" alt="" v-show="index === 0">
          </div>
          <el-avatar :src="item.avatar"></el-avatar>

          <div class="m_item_box">
            {{ item.content }}
            <i class="el-icon-close" v-if="item.uid === $store.state.user.id" @click="del(item.id)"></i>

          </div>
        </div>
      </div>
    </div>
    <div class="fb_box">
      <div class="left">
        <el-input v-model="input" placeholder="发表您的留言" :maxlength="20"></el-input>
      </div>
      <button class="right_button" @click="leave">发表</button>
    </div>

  </div>
</template>

<style scoped>
.message_box {
  overflow-x: hidden;
  overflow-y: auto;
  height: 300px;
}

.message_item {
  display: inline-flex;
  font-size: 20px;
  color: #fff;
  background-color: rgba(0, 0, 0, 0.7);
  padding: 10px 12px;
  border-radius: 7px;
  margin: 10px 20px;

  .one {
    position: absolute;
    height: 30px;
    width: 30px;
    top: -15px;
    left: -3px;
  }

  .el-avatar {
    height: 40px;
    width: 40px;
  }

  .m_item_box {
    display: inline-block;
    height: 40px;
    line-height: 40px;
    margin-left: 10px
  }

  .el-icon-close {
    cursor: pointer;
  }

  .el-icon-close:hover {
    color: #f44336;
  }

  animation: scroll 10s linear;
}

@keyframes scroll {
  from {
    opacity: 0;
    transform: translateX(200%);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

.fb_box {
  height: 40px;
  padding-top: 10px;
  display: flex;
  justify-content: space-between;

  .left {
    width: 750px;
    margin-left: 20px;
  }

  .right_button {
    border: none;
    width: 100px;
    cursor: pointer;
    height: 40px;
    margin-right: 20px;
    background-color: #409EFF;
    color: #ffffff;
    border-radius: 10px;
  }
}
</style>